/**
 * This software is distributed under the terms of the MIT License.
 * Copyright (c) 2020 CrobTech.
 * Author: Jason Zhu
 * Contributors: https://gitee.com/JasonZhu2021/tmf8801_tof_demo
 */

/**************************************************************************************
 * INCLUDE
 **************************************************************************************/

#include "TCA9548A_Generic.h"

/**************************************************************************************
 * PUBLIC MEMBER FUNCTIONS
 **************************************************************************************/
/**
 * @brief                   注册绑定总线IO硬件接口
 * @param  pObj             TMF8801 对象
 * @param  pIO              TMF8801 IO对象
 * @return int32_t          注册绑定结果
 */
int32_t TCA9548A_RegisterBusIO(TCA9548A_Object_t *pObj, TCA9548A_IO_t *pIO)
{
  int32_t ret = TCA9548A_OK;
  if(pObj == NULL)
  {
    ret = TCA9548A_ERROR;
  }
  else
  {
    pObj->IO.Init       = pIO->Init;
    pObj->IO.Address    = pIO->Address;
    pObj->IO.WriteData   = pIO->WriteData;
    pObj->IO.ReadData    = pIO->ReadData;

    if(pObj->IO.Init == NULL)
    {
      ret = TCA9548A_ERROR;
    }
    else if(pObj->IO.Init() != TCA9548A_OK)
    {
      ret = TCA9548A_ERROR;
    }
  }
  return ret;
}

/**
 * @brief                   设置IIC选择通道
 * @param  pObj             TCA9548A 对象
 * @param  Id               获取的选通 通道值
 * @return int32_t          获取状态结果
 */
int32_t TCA9548A_WriteChID(TCA9548A_Object_t *pObj, IIC_Channel ch)
{
  int32_t ret;
  uint8_t ch_val = ch;
  ret = pObj->IO.WriteData(pObj->IO.Address,&ch_val,1U);
  return ret;
}
/**
 * @brief                   获取IIC选择通道
 * @param  pObj             TCA9548A 对象
 * @param  Id               获取的选通 通道值
 * @return int32_t          获取状态结果
 */
int32_t TCA9548A_ReadChID(TCA9548A_Object_t *pObj)
{
  int32_t ret;
  uint8_t ch_val;
  ret = pObj->IO.ReadData(pObj->IO.Address,&ch_val,1U);
  pObj->ChId = (IIC_Channel) ch_val;
  return ret;
}


